// import experience from "../../assets/experience";
// const utils = require("../../utils/util");
// const cloud = require("../../utils/api.js");
const { getCloudFunctionData } = require("../../utils/cloudRequest.js");
const app = getApp();

Page({
  data: {
    page: 1,
    pageSize: 20,
    articleList: [],
    hide: app.globalData.hide,
    theme: "",
    scrollTop: 0,
    themeType: app.globalData.themeType,
  },

  internet(e) {
    const { page } = e.currentTarget.dataset;
    switch (page) {
      case "map":
        wx.navigateTo({
          url: "/packageIndex/pages/map/map",
        });
        break;
      case "color":
        wx.navigateTo({
          url: "/packageIndex/pages/color/color",
        });
        break;
      case "base64":
        wx.navigateTo({
          url: "/packageIndex/pages/base64/index",
        });
        break;
      case "code":
        wx.navigateTo({
          url: "/packageIndex/pages/code/code",
        });
        break;

      default:
        wx.showToast({
          title: "开发中",
          icon: "none",
        });
        break;
    }
  },

  goDetail(e) {
    const { id, type, article, title } = e.currentTarget.dataset;
    wx.navigateTo({
      url:
        "/packageIndex/pages/detail/detail?id=" + id + "&type=" + type + "&title=" + title + "&article=" + article,
    });
  },

  // 共享数据
  async getArticleData() {
    let { page, pageSize, articleList } = this.data;
    const res = await getCloudFunctionData({ gather: 'article', page, pageSize, order: { name: 'time', sort: 'desc' } })
    if (res.data && res.data.length > 0) {
      page++;
      articleList = articleList.concat(res.data);
      this.setData({
        page,
        articleList
      })
    } else if (articleList.length != 0 && res.data.length <= 0) {
      wx.showToast({
        title: '没有更多了~',
        icon: 'none'
      })
    }
  },

  scrollTop() {
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 300,
    });
  },
  onPageScroll(e) {
    this.setData({
      scrollTop: e.scrollTop,
    });
  },

  goToMusic() {
    if (this.data.hide) {
      wx.navigateTo({
        url: "/packageMore/pages/music/music",
      });
    } else {
      wx.navigateTo({
        url: "/packageMore/pages/music/index/index",
      });
    }
  },

  // 事件处理函数
  onLoad() {
    this.getArticleData();
  },

  onShow() {
    if (typeof this.getTabBar === "function" && this.getTabBar()) {
      this.getTabBar().setData({
        selected: 0,
        themeType: app.globalData.themeType,
      });
    }
    this.setData({
      themeType: app.globalData.themeType,
      hide: app.globalData.hide
    });

    const themeType = wx.getStorageSync("themeType");
    if (themeType == "dark") {
      wx.setNavigationBarColor({
        backgroundColor: "#121212",
        frontColor: "#ffffff",
      });
      wx.setBackgroundColor({
        backgroundColor: '#121212', // 窗口的背景色
        backgroundColorTop: '#121212', // 顶部窗口的背景色
        backgroundColorBottom: '#121212', // 底部窗口的背景色
      })
    } else {
      wx.setNavigationBarColor({
        backgroundColor: "#ffffff",
        frontColor: "#000000",
      });
      wx.setBackgroundColor({
        backgroundColor: '#ffffff', // 窗口的背景色
        backgroundColorTop: '#ffffff', // 顶部窗口的背景色
        backgroundColorBottom: '#f6f7f9', // 底部窗口的背景色
      })
    }
  },

  onReachBottom() {
    this.getArticleData();
  },

  onPullDownRefresh() { },

  //分享给好友
  onShareAppMessage() { },
  // 分享到朋友圈
  onShareTimeline() { },
});
